package com.example.demo.proxy;



import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLibMethodInterceptor implements MethodInterceptor {

    //目标对象/被代理对象
    private Object target;

    public CGLibMethodInterceptor(HouseSubject target){
        this.target = target;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        //代理前
        System.out.println("开始代理");
        //通过反射执行目标对象的目标方法
        Object result = method.invoke(target, objects);
        //代理后
        System.out.println("结束代理");
        return result;
    }
}
